package com.fucking.great.context.common.db;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import com.fucking.great.context.TxnContext;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class JDBCSession extends Session {
    public JDBCSession(DataSource ds) {
        super(ds);
    }
    private int security = 1;

    @Override
    public int insertOrUpdate(Entity record, String... keys) throws SQLException {
//        fillByInsert(record);
        return super.insertOrUpdate(record, keys);
    }

    @Override
    public int[] insert(Collection<Entity> records) throws SQLException {
        if (CollectionUtil.isEmpty(records)) {
            return new int[0];
        }
        records.forEach(x -> fillByInsert(x));
        return super.insert(records);
    }

    @Override
    public int insert(Entity record) throws SQLException {
//        fillByInsert(record);
        return super.insert(record);
    }

    @Override
    public Long insertForGeneratedKey(Entity record) throws SQLException {
//        fillByInsert(record);
        return super.insertForGeneratedKey(record);
    }



    @Override
    public List<Object> insertForGeneratedKeys(Entity record) throws SQLException {
//        fillByInsert(record);
        return super.insertForGeneratedKeys(record);
    }


    @Override
    public int del(Entity where) throws SQLException {
//        fillByDelete(where);
        return super.del(where);
    }

    @Override
    public int update(Entity record, Entity where) throws SQLException {
//        fillByUpdate(record,where);
        return super.update(record, where);
    }


    /**
     * 查询List<String>
     * slect id from user;
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public List<Object> querySet(String sql, Object... params) throws SQLException {
        List<Object> query = this.query(sql, RowHandler.ROW, params);
        return query;
    }
    /**
     * 查询List<String>
     * slect id from user;
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public List<Object> querySet(String sql, Map<String, Object> params) throws SQLException {
        List<Object> query = this.query(sql, RowHandler.ROW, params);
//        this.find();
//        Entity where = null;
//        where.setFieldNames();
        return query;
    }

    /**
     * context 转化 Entity
     * 里面的table -> 为表名。
     * @param context
     * @return
     */
    public Entity context2entity(Map<String,Object> context){
        //
        Entity entity = new Entity();
        context.entrySet().forEach( o -> {
            String key = o.getKey();
            Object value = o.getValue();
            if("table".equals(key)){
                entity.setTableName(String.valueOf(value));
            }else{
                entity.setIgnoreNull(key,value);
            }
        } );
        return entity;
    }

    /**
     * 填充插入
     * @param record
     */
    public void fillByInsert(Entity record){
        if(this.security > 0){
            TxnContext context = TxnContext.getContext();
            record.set("appid",context.getAppid());
            record.set("system_name",context.getSystemName());
            record.set("service_ip",context.SERVICE_IP);
            record.set("client_ip",context.getClientIp());
            record.set("url",context.getUrl());
            record.set("create_time", DateUtil.now());
        }
    }

    public void fillByUpdate(Entity record,Entity where){
        if(this.security > 0){
            TxnContext context = TxnContext.getContext();
            record.set("update_time", DateUtil.now());
            record.set("service_ip",context.SERVICE_IP);
            record.set("client_ip",context.getClientIp());
            record.set("url",context.getUrl());
            // where
            where.set("appid",context.getAppid());
            where.set("system_name",context.getSystemName());
        }
    }

    public void fillByDelete(Entity record){
        // 删除必须添加  appid && system_name ;
        if(this.security > 0){
            TxnContext context = TxnContext.getContext();
            record.set("appid",context.getAppid());
            record.set("system_name",context.getSystemName());
        }
    }




    public int getSecurity() {
        return security;
    }

    public void setSecurity(int security) {
        this.security = security;
    }


}
